/**
 * 登录注册找回密码API
 *
 * @版权所有 rem.studio
 */
import request from '@/utils/request';
import { getParamsSuffix } from '@/utils/paramsUtils';

export async function query() {
  return request('/api/account/users');
}

export async function getCurrentUser() {
  const cancersystem_token = localStorage.getItem('cancersystem_token');
  if (cancersystem_token) {
    return request('/api/uaa/users/getSysUser', {
      method: 'GET',
    });
  }
}

/**
 * 执行单点登录模式下的用户登录
 * @param query
 * @returns {Promise<any>}
 */
export async function getToken(query) {
  const params = getParamsSuffix(query);
  const resp = await request(`/api/cdcerservice/v1/tkmSysUser/gettoken?${params}`, {
    method: 'GET',
  });
  if (resp && resp.data) {
    const { data } = resp;
    localStorage.setItem('cancersystem_token', data.access_token);
    // 查询当前用户的授权菜单和按钮
    const currentAuthorityResponse = request(`/api/account/sysuserdetail/queryCurrentAuthority`, {
      method: 'POST',
      requestType: 'form',
      data: {
        username: data.username,
        mobile: null,
      },
    });
    currentAuthorityResponse.then(data => {
      if (data) {
        localStorage.setItem('cancersystem_menu', JSON.stringify(data.menu));
        localStorage.setItem('cancersystem_menubutton', data.menuButton); // 当前用户的授权按钮隐藏项
      }
    });
    return currentAuthorityResponse;
  } else {
    localStorage.removeItem('cancersystem_token');
    localStorage.removeItem('cancersystem_menu');
    localStorage.removeItem('cancersystem_menubutton');
    localStorage.removeItem('cancersystem_tid');
    localStorage.setItem('cancersystem_authority', 'guest');
    return {
      status: 'error',
      message: resp.message,
    };
  }
}

/** 登录注册找回密码API开始 */

export async function loginAccount(params) {
  params['scope'] = 'ui';
  // if (params.smsCaptcha) {
  //   params['grant_type'] = 'mobile';
  // } else {
  //   params['grant_type'] = 'password';
  // }
  params['grant_type'] = 'password';
  params['client_id'] = 'browser';
  const data = await request('/api/uaa/oauth/token', {
    method: 'POST',
    requestType: 'form',
    headers: {
      Authorization: 'Basic YnJvd3Nlcjo=',
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: params,
  });
  if (data && !data.error) {
    localStorage.setItem('cancersystem_token', data.access_token);
    // 查询当前用户的授权菜单和按钮
    const currentAuthorityResponse = request(`/api/account/sysuserdetail/queryCurrentAuthority`, {
      method: 'POST',
      requestType: 'form',
      data: {
        username: params.username,
        mobile: params.mobile,
      },
    });
    currentAuthorityResponse.then(data => {
      if (data) {
        localStorage.setItem('cancersystem_menu', JSON.stringify(data.menu));
        localStorage.setItem('cancersystem_menubutton', data.menuButton); // 当前用户的授权按钮隐藏项
      }
    });
    return currentAuthorityResponse;
  } else {
    localStorage.removeItem('cancersystem_token');
    localStorage.removeItem('cancersystem_menu');
    localStorage.removeItem('cancersystem_menubutton');
    localStorage.removeItem('cancersystem_tid');
    localStorage.setItem('cancersystem_authority', 'guest');
  }
}

export async function registerAccount(params) {
  return request('/api/account/sysuserdetail/registerAccount', {
    method: 'POST',
    data: params,
  });
}

export async function retrievePassword(params) {
  return request('/api/account/sysuserdetail/retrievePassword', {
    method: 'POST',
    requestType: 'form',
    data: {
      ...params,
    },
  });
}

export async function compareCaptcha(params) {
  return request('/api/account/sysuserdetail/compareCaptcha', {
    method: 'POST',
    requestType: 'form',
    data: {
      ...params,
    },
  });
}

export async function getSmsCaptcha(mobile) {
  return request(`/api/account/captcha/getSmsCaptcha?mobile=${mobile}`);
}

/** 登录注册找回密码API结束 */
